package is.billsharing.app;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class GroupActivity extends Activity

{
	private static final String PREFS_NAME = "user";
	Button add_grp,addUserToGroup,deleteUserFromGroup;
	EditText grp_name;
	TextView tv;
	int gid;
	int uid;
	String groupName;
	String[] friends;
	 SharedPreferences settings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.group);

		add_grp = (Button) findViewById(R.id.addgrp);
		grp_name = (EditText) findViewById(R.id.grpname);
		tv = (TextView) findViewById(R.id.textView1);
		settings = getSharedPreferences(PREFS_NAME, 0);
		  uid = settings.getInt("uid", 0);
		add_grp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("gname", grp_name.getText().toString()));
				postParameters.add(new BasicNameValuePair("uid", "" + uid + ""));
				String response1;
				try {
					response1 = CustomHttpClient.executeHttpPost("AddGroup",postParameters);
					String res = response1.toString();
					tv.setText(res);
					JSONObject json = new JSONObject(res);
					gid = json.getInt("gid");
					groupName = json.getString("groupName");
					tv.setText(groupName);
					SharedPreferences.Editor editor = settings.edit();
					editor.putInt("gid", gid);
					editor.commit();
					Intent intent=new Intent(GroupActivity.this,AddGrpMem.class);
        	    	startActivity(intent);

				}

				catch (Exception e) {
					e.printStackTrace();
				}
			}
			});
		
	

	
	}


}
